//
//  LBMStatusCell.swift
//  微博
//
//  Created by 刘保满 on 16/5/24.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class LBMStatusCell: UITableViewCell {
    
    
    
    //声明约束对象属性,用来暂时保存底部工具条的约束
    var toolbarTopCons: Constraint?

    //cell的视图模型属性
    var statusViewModel: LBMStatusViewModel? {
        
        didSet {
            
            statusOriginalView.statusViewModel = statusViewModel
            
            statusToorBar.statusViewModel = statusViewModel
            //在这里要判断是否是转发微博,如果是原创微博,就要让转发微博视图隐藏,并且要刷新底部工具条的约束
            //在这里先把工具条的原来的顶部约束卸载
            toolbarTopCons?.uninstall()
            if statusViewModel?.status?.retweeted_status == nil {
                
                //说明没有转发微博,隐藏转发微博视图
                statusRetweetedView.statusViewModel = nil
                statusRetweetedView.hidden = true
                //更新底部工具条的顶部约束
                statusToorBar.snp_updateConstraints(closure: { (make) in
                    
                        self.toolbarTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                
                
            } else {
                
                //说明是转发微博,设置转发微博视图模型,显示
                statusRetweetedView.statusViewModel = statusViewModel
                
                statusRetweetedView.hidden = false
                
                //修改底部工具条的约束
                
                statusToorBar.snp_updateConstraints(closure: { (make) in
                    
                self.toolbarTopCons =  make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                    
                })
          
            }
            
            
            
        }
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //        self.backgroundColor = randomColor()
        self.selectionStyle = .None
        setupUI()
        
    }
    
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI
    private func setupUI() {
        
        //添加原创微博视图
        contentView.addSubview(statusOriginalView)
        //添加转发微博视图
        contentView.addSubview(statusRetweetedView)
        
        //添加底部工具条
        contentView.addSubview(statusToorBar)
        
        
        
        //设置原创微博视图的约束
        statusOriginalView.snp_makeConstraints { (make) in
            
            make.top.right.left.equalTo(self.contentView)
            
//            make.height.equalTo(160)
        }
        
        //设置转发微博的约束
        statusRetweetedView.snp_makeConstraints { (make) in
            
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(self.contentView)
//            make.height.equalTo(60)
            
            
        }
        
        
        //设置底部工具条视图的约束
        statusToorBar.snp_makeConstraints { (make) in
            //用定义的临时属性保存顶部约束
           self.toolbarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            
            make.left.right.equalTo(self.contentView)
            make.height.equalTo(36)
            
        }
        
        
        //给cell的contenview设置底部约束
        contentView.snp_makeConstraints { (make) in
            
            //make,会把frame全部取消,所以要把contentview的约束全都设置
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(statusToorBar.snp_bottom)
            
        }

    }
    
    //懒加载原创微博视图
    private lazy var statusOriginalView: LBMStatusOriginalView = LBMStatusOriginalView()
    
    //懒加载转发微博视图
    private lazy var statusRetweetedView: LBMStatusRetweetedView = LBMStatusRetweetedView()
    
    //懒加载底部工具条视图
    private lazy var statusToorBar: LBMStatusToolBar = LBMStatusToolBar()
    
    
    
}
